package com.keymozy.controller;

import com.keymozy.pojo.Borrow;
import com.keymozy.pojo.MChild;
import com.keymozy.pojo.Model;
import com.keymozy.pojo.Zone;
import com.keymozy.service.MChildService;
import com.keymozy.state.Code;
import com.keymozy.state.RespData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/mchild")
@CrossOrigin
public class MChildController {
    @Autowired
    private MChildService mChildService;
    @GetMapping("/getList")
    public RespData getList(int currentPage,int pageSize,Integer flag, Model model){
        log.info("model:"+model);
        log.info("flag:"+flag);
        Map<String, Object> result = mChildService.getList(currentPage, pageSize, flag, model);
        if (result.isEmpty())
            return new RespData("9999","没有结果",null);
        return new RespData(Code.SUCCESS_SEARCH,result);
    }

    @GetMapping("/borrow/{mid}/{sid}")
    public RespData borrow(@PathVariable("mid") Integer mid,@PathVariable("sid") Integer sid){
        log.info("mid:"+mid);
        log.info("sid:"+sid);
        boolean flag = mChildService.borrow(mid, sid);
        if (flag){
            return new RespData("0005","借用成功！",null);
        }
        return new RespData(Code.FALSE_BORROW,null);
    }

    @GetMapping("/borrow/{sid}")
    public RespData borrow(@PathVariable("sid") Integer sid){
        List<Borrow> borrow = mChildService.getBorrow(sid);
        if (borrow == null)
            return new RespData(Code.FALSE_SEARCH,null);
        else
            return new RespData(Code.SUCCESS_SEARCH,borrow);
    }

    /**
     * 设置模具状态
     * @param mid
     * @param flag
     * @return
     */
    @GetMapping("/setState/{mid}/{flag}")
    public RespData setState(@PathVariable("mid") Integer mid,@PathVariable("flag") Integer flag){
        log.info("正在设置模具状态。。。。。");
        if(mChildService.setState(mid,flag))
            return new RespData(Code.SUCCESS_UPDATE_1,true);
        return new RespData(Code.FALSE_UPDATE,null);
    }

    /**
     * 归还
     * @param borrow
     * @return
     */
    @PostMapping("/borrow")
    public RespData toReturn(@RequestBody Borrow borrow){
        boolean b = mChildService.toReturn(borrow);
        if (b)
            return new RespData(Code.SUCCESS_RETURN,borrow);
        else
            return new RespData(Code.FALSE_RETURN,null);
    }
    @GetMapping("/statistics")
    public RespData statisticsModelState(){
        return new RespData(Code.SUCCESS_SEARCH,mChildService.statistics());
    }
}
